package com.chenlixian.fishandbearpaw.controller;

import com.chenlixian.fishandbearpaw.entity.Dto.UserLoginDto;
import com.chenlixian.fishandbearpaw.entity.Vo.GeneralVO;
import com.chenlixian.fishandbearpaw.entity.enumEntity.ErrorListEnum;
import com.chenlixian.fishandbearpaw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author chenlixian
 * @description TODO 处理用户请求控制器
 * @date 2020/3/15 17:06
 */
@Controller
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 登录
     * @param userLoginDto
     * @return
     **/
    @RequestMapping("/login")
    @ResponseBody
    public GeneralVO login (@RequestBody UserLoginDto userLoginDto){
        UserLoginDto user = userService.login(userLoginDto);
        if (user == null)
            return new GeneralVO(ErrorListEnum.E20009,"账号或密码错误");
        else {
            return new GeneralVO(ErrorListEnum.E200,user);
        }
    }
    @RequestMapping("/register")
    @ResponseBody
    public GeneralVO register (@RequestBody UserLoginDto userLoginDto){
        String result = userService.register(userLoginDto);
        if (result.equals("注册成功"))
            return new GeneralVO(ErrorListEnum.E200,result);
        else if (result.equals("用户已存在"))
            return new GeneralVO(ErrorListEnum.E60001,result);
        else
            return new GeneralVO(ErrorListEnum.E30002,result);
    }
    @RequestMapping("/favorite")
    @ResponseBody
    public GeneralVO favorite (@RequestBody Map<String,Integer> map) {
        Integer userId = map.get("userId");
        Integer fUserId= map.get("fUserId");
        Integer isFavorite = map.get("isFavorite");
        try {
            GeneralVO result = userService.favorite(userId, fUserId,isFavorite);
            if (result != null)
                return result;
            else
                return new GeneralVO(ErrorListEnum.E20006,"操作失败");
        }
        catch (Exception ex){
            return new GeneralVO(ErrorListEnum.E20006,"操作失败");
        }


        }
    @RequestMapping("/queryUserRandom")
    @ResponseBody
    public GeneralVO queryUserRandom (@RequestBody Map<String,Integer> map) {
        Integer userId = map.get("userId");
        List<UserLoginDto> users = userService.queryUserRandom(userId);
        return new GeneralVO(ErrorListEnum.E200,users);
    }
}